<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/index/text4.css" />
		<style>
		</style>
	</head>

	<body>
		<div id="pullrefresh" class="mui-content mui-scroll-wrapper">
			<div class="mui-content">
			  <div class="mui-row box-top" id="reback">
			        <div class="mui-col-sm-6 mui-col-xs-12 title-box">
			            <i class="mui-icon mui-icon-back"></i>
			        </div>
			        <div class="mui-col-sm-6 mui-col-xs-12">
			        </div>
			    </div>
			</div>
			<!--  -->
			<div>5s5d45s4d54s5d<br/>5s5d45s4d54s5ds5d45s4d54s5d<br/>s5d45s4d54s5d<br/>s5d45s4d54s5d<br/></div>

			<div>
				<img id="bitm" src="" alt="" />
			</div>
			<div class="caremam-btn-con">
				<div class="mui-row box-top" id="reback">
			        <div class="mui-col-sm-6 mui-col-xs-6">
			            <button type="button" class="mui-btn mui-btn-success" onclick="captureWebview()" id="blool">截屏</button>
			        </div>
			        <div class="mui-col-sm-6 mui-col-xs-6">
			            <button type="button" class="mui-btn mui-btn-success" onclick="captureCreate()" id="blools">裁剪</button>
			        </div>
			 	</div>
			</div>
		</div>
	</body>
	<script src="../js/html2canvas.js"></script>
	<script src="../js/jquery-1.9.1.js"></script>
	<script src="../js/mui.min.js"></script>
	<script>

			mui.init();
			document.getElementById('reback').addEventListener('tap', function() {
					mui.back()
			})

			var ws=null;
			// H5 plus事件处理
			function plusReady(){
				ws=plus.webview.currentWebview();
	
			}
			if(window.plus){
				plusReady();
			}else{
				document.addEventListener('plusready', plusReady, false);
			}
			// 截屏绘制
//			var bitmap=null;
			function captureWebview() {
//				var imgd = document.getElementById('bitm')
//				bitmap = new plus.nativeObj.Bitmap('test');
	//			bitmap.loadBase64Data(dataUrl,function(){
	          	html2canvas(document.getElementsByTagName("body"), {
                    allowTaint: true,
                    taintTest: false,
                    onrendered: function(canvas) {
                        canvas.id = "mycanvas";
                        //document.body.appendChild(canvas);
                        //生成base64图片数据
                       var dataUrl = canvas.toDataURL();
						plus.gallery.save( '../images', function (e) {
							console.log(JSON.stringify(e))
							console.log( "保存图片到相册成功" );
						},function(e){
							console.log(JSON.stringify(e))
							console.log( "保存图片到相册失败" );
						});
						$("#bitm").attr("src",dataUrl);
                       }
                   });
				}
			
				function captureCreate() {
					var IMAGE_UNSPECIFIED = "image/*";
	                var PHOTOZOOM = 2; // 获取完图片返回key
	                var PHOTOLAT = 1; // 剪裁完毕后返回key
	                var main = plus.android.runtimeMainActivity();
	                var Intent = plus.android.importClass("android.content.Intent");
	                var MediaStore = plus.android.importClass("android.provider.MediaStore");
	                var File = plus.android.importClass("java.io.File");
	                var Uri = plus.android.importClass("android.net.Uri");
	                var intent = new Intent(Intent.ACTION_PICK, null);
	                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
	                var outPutPath = plus.io.convertLocalFileSystemURL("__downloads/5566.jpg");
	                main.startActivityForResult(intent, PHOTOZOOM);
	                main.onActivityResult = function(requestCode, resultCode, data) {
	                    if (PHOTOZOOM == requestCode) {
	                        var file = new File(outPutPath);
	                        // 输出目录uri
	                        var outPutUri = Uri.fromFile(file);
	                        plus.android.importClass(data);
	                        var uri = data.getData();
	                        var cropIntent = new Intent("com.android.camera.action.CROP");
	                        cropIntent.setDataAndType(uri, IMAGE_UNSPECIFIED);
	                        // 截图完毕后 输出目录
	                        cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
	                        cropIntent.putExtra("crop", "true");
	                        // aspectX aspectY 是宽高的比例
	                        cropIntent.putExtra("aspectX", 1);
	                        cropIntent.putExtra("aspectY", 1);
	                        // outputX outputY 是裁剪图片宽高
	                        cropIntent.putExtra("outputX", 64);
	                        cropIntent.putExtra("outputY", 64);
	                        cropIntent.putExtra("return-data", true);
	                        main.startActivityForResult(cropIntent, PHOTOLAT);
	                    } else if (requestCode == PHOTOLAT) {
	                        // 判断 剪裁完后的图片输出是否存在
	                        var file = new File(outPutPath);
	                        var a = file.exists();
	                        console.log(JSON.stringify(file))
	                    }
	                };
				}

		
	</script>
</html>